package com.cloudcc.frame.Interface;

import com.cloudcc.deveditor.core.CCObject;
import com.cloudcc.deveditor.core.CCService;
import com.cloudcc.deveditor.core.ServiceResult;
import com.cloudcc.deveditor.core.UserInfo;
import net.sf.json.JSONObject;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

public class getmanageroleTest {
    public void tset(HttpServletRequest request) {

        //这是一个Site页面，可以通过URL直接访问 上面是测试环境
//        ServiceResult sr = CCService.getUserInfo("61ee0c8c32fe6ea38850a3475fa15750fbc1bb1a20768b71", "test_hhtest@cloudcc.com", "111111");
        ServiceResult sr = null;
        //ServiceResult sr = CCService.getUserInfo("f6c729c175119d3aa2942d814a63df8564dfe85e8959db2d","interface@cloudcc.com","111111");
        UserInfo uuserInfo = (UserInfo) sr.get("COMMON_USERINFO");
        CCService cs = new CCService(uuserInfo);
        String msg = "";
        String managementType = "已失效";
        String resultMessage = "SUCCESS";
        String resultMessageError = "ERROR";
        String resultCode = "0000";
        String date = request.getParameter("data") == null ? "" : request.getParameter("data") + "";

        String invokeId = request.getParameter("invokeId") == null ? "" : request.getParameter("invokeId") + "";
        JSONObject dataInfos = JSONObject.fromObject(date);
        JSONObject returnDateInfo = new JSONObject();

        CCObject interface_record = new CCObject("interface_record");
        interface_record.put("url", "getmanagerole.jsp");
        interface_record.put("record_ID", invokeId);
        interface_record.put("datas", date);
        try {
            String scode = dataInfos.get("scode") == null ? "" : dataInfos.get("scode") + "";
            String psmc_num = dataInfos.get("psmc_num") == null ? "" : dataInfos.get("psmc_num") + "";
            String findSql ="select id from psmc where uncode ='"+psmc_num+"' and is_deleted='0' ";
            List<CCObject> psmc = cs.cqlQuery("psmc", findSql);
           if (psmc.size()>0) {
               psmc_num =   psmc.get(0).get("id")==null?"": psmc.get(0).get("id")+"";
           }
            String findStewardInfoSql = "select id,fk_ccuser,ghzt,get_time,createdate from manage_group " +
                    "where name ='" + scode + "' and fk_psmc ='" + psmc_num + "' " +
                    "and is_deleted='0' and ghzt !='" + managementType + "' ";
            List<CCObject> manage_group = cs.cqlQuery("manage_group", findStewardInfoSql);
            returnDateInfo.put("invokeId", invokeId);
            returnDateInfo.put("resultCode", resultCode);
            returnDateInfo.put("resultMessage", resultMessage);
            JSONObject dataInfo = new JSONObject();
            if (manage_group.size() > 0) {
                dataInfo.put("user_code", manage_group.get(0).get("fk_ccuser"));
                dataInfo.put("status", manage_group.get(0).get("ghzt"));
                dataInfo.put("startdate", manage_group.get(0).get("get_time"));
                dataInfo.put("createdate", manage_group.get(0).get("createdate"));
            }
            returnDateInfo.put("data", dataInfo);
            interface_record.put("requestDataLong", returnDateInfo);
            cs.insert(interface_record);
            msg = String.valueOf(returnDateInfo);

        } catch (Exception e) {
            returnDateInfo.put("invokeId", invokeId);
            returnDateInfo.put("resultCode", resultCode);
            returnDateInfo.put("resultMessage", resultMessageError);
            returnDateInfo.put("data", "data中的scode或者psmc_num为空，请检查传参");
            interface_record.put("data", returnDateInfo);
            cs.insert(interface_record);
            msg=String.valueOf(returnDateInfo);
        }
        request.setAttribute("jsonmsg", msg);
    }
}
